﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class NX_DETAI_Form : Form
    {
        NX_DETAI_Data nxdetai_Data = new NX_DETAI_Data();
        DETAI_Data detai_Data = new DETAI_Data();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        NX_DETAI nxdetai = new NX_DETAI();
        

        public NX_DETAI_Form()
        {
            InitializeComponent();
        }

        private void NX_DETAI_Form_Load(object sender, EventArgs e)
        {
            txtMaNXDT.Text = "NXDT";
            txtDanhGia.Text = "";
            txtGhiChu.Text = "";
            txtSoTietChuan.Text = "";
            txtNamHoc.Text = DateTime.Now.Year.ToString();

            cbDeTai.Text = "";
            cbGiaoVien.Text = "";
            cbDeTai.Items.Clear();
            cbGiaoVien.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            detai_Data.detai_Combobox(cbDeTai);
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            nxdetai.hienthi_NXDeTai(dtgNXDT);

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaNXDT.Text == "")
            {
                MessageBox.Show("ban chua nhap Ma Nhan Xet De Tai");
                txtMaNXDT.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien nhan xet");
                return;
            }
            else if (cbDeTai.Text == "")
            {
                MessageBox.Show("Ban chua chon de tai");
                return;
            }
            else if (txtGhiChu.Text == "")
            {
                MessageBox.Show("ban chua nhap ghi chu");
                txtGhiChu.Focus();
                return;
            }
            else if (txtDanhGia.Text == "")
            {
                MessageBox.Show("ban chua nhap danh gia");
                txtDanhGia.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("ban chua nhap so tiet chuan");
                txtSoTietChuan.Focus();
                return;
            }
            else if (nxdetai_Data.kiemtra_GiaoVienNX(cbGiaoVien.Text,((ListItem)(cbDeTai.SelectedItem)).value))
            {
                MessageBox.Show("Giao vien khong the vua huong dan vua nhan xet");
                return;
            }
            else
            {
                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0)
                    {
                        MessageBox.Show("So tiet chuan chua hop le");
                        return;
                    }
                    else
                        nxdetai.sotietchuan = (int)(Convert.ToInt16(txtSoTietChuan.Text));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("So tiet chuan chua hop le \n" + ex.Message);
                }
                nxdetai.maNXDT = txtMaNXDT.Text.ToString();
                nxdetai.danhgia = txtDanhGia.Text.ToString();
                nxdetai.ghichu = txtGhiChu.Text.ToString();

                nxdetai.maDT = ((ListItem)(cbDeTai.SelectedItem)).value;
                nxdetai.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;

                nxdetai.them_NXDeTai(nxdetai);
                nxdetai.hienthi_NXDeTai(dtgNXDT);

                txtMaNXDT.Text = "NXDT";
                txtDanhGia.Text = "";
                txtGhiChu.Text = "";
                txtSoTietChuan.Text = "";
                txtNamHoc.Text = DateTime.Now.Year.ToString();

                cbDeTai.Text = "";
                cbGiaoVien.Text = "";
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaNXDT.Text == "")
            {
                MessageBox.Show("ban chua nhap Ma Nhan Xet De Tai");
                txtMaNXDT.Focus();
                return;
            }
            else if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien nhan xet");
                return;
            }
            else if (cbDeTai.Text == "")
            {
                MessageBox.Show("Ban chua chon de tai");
                return;
            }
            else if (txtGhiChu.Text == "")
            {
                MessageBox.Show("ban chua nhap ghi chu");
                txtGhiChu.Focus();
                return;
            }
            else if (txtDanhGia.Text =="")
            {
                MessageBox.Show("ban chua nhap danh gia");
                txtDanhGia.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("ban chua nhap so tiet chuan");
                txtSoTietChuan.Focus();
                return;
            }

            else
            {
                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0)
                    {
                        MessageBox.Show("So tiet chuan chua hop le");
                        return;
                    }
                    else
                        nxdetai.sotietchuan = (int)(Convert.ToInt16(txtSoTietChuan.Text));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("So tiet chuan chua hop le \n" + ex.Message);
                }
                nxdetai.maNXDT = txtMaNXDT.Text.ToString();
                nxdetai.danhgia = txtDanhGia.Text.ToString();
                nxdetai.ghichu = txtGhiChu.Text.ToString();

                nxdetai.maDT = ((ListItem)(cbDeTai.SelectedItem)).value;
                nxdetai.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;

                nxdetai.sua_NXDeTai(nxdetai);
                nxdetai.hienthi_NXDeTai(dtgNXDT);

                txtMaNXDT.Text = "NXDT";
                txtDanhGia.Text = "";
                txtGhiChu.Text = "";
                txtSoTietChuan.Text = "";
                txtNamHoc.Text = DateTime.Now.Year.ToString();

                cbDeTai.Text = "";
                cbGiaoVien.Text = "";
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaNXDT.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Nhan Xet De Tai");
                txtMaNXDT.Focus();
                return;
            }
            else
            {
                nxdetai.xoa_NXDeTai(txtMaNXDT.Text.ToString());
                nxdetai.hienthi_NXDeTai(dtgNXDT);
            }
        }

        private void dtgNXDT_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            cbDeTai.Items.Clear();
            cbGiaoVien.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            detai_Data.detai_Combobox(cbDeTai);

            dtgNXDT.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaNXDT.Text = dtgNXDT.Rows[dong].Cells[0].Value.ToString();
            txtDanhGia.Text = dtgNXDT.Rows[dong].Cells[3].Value.ToString();
            txtGhiChu.Text = dtgNXDT.Rows[dong].Cells[4].Value.ToString();
            txtSoTietChuan.Text = dtgNXDT.Rows[dong].Cells[5].Value.ToString();

            String madetai = dtgNXDT.Rows[dong].Cells[1].Value.ToString();
            nxdetai_Data.layten_DeTai(madetai, cbDeTai);
            String magiaovien = dtgNXDT.Rows[dong].Cells[2].Value.ToString();
            nxdetai_Data.layten_GiaoVien(magiaovien, cbGiaoVien);

        }





        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            int hk=0;
            int namhoc = 0;
            cbDeTai.Items.Clear();
            if (nmHocKi.Value <= 0 || nmHocKi.Value >= 20)
            {
                MessageBox.Show("Ban chon sai hoc ki");
                return;
            }
            else
            {
                try
                {
                    hk=(int)nmHocKi.Value;
                    if ((namhoc=Convert.ToInt16(txtNamHoc.Text)) <= 1990)
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                    detai_Data.detaitheonamhoc_Combobox(cbDeTai, hk, namhoc);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban nhap sai du lieu \n" + ex.Message);
                }
            }


            String magiaovien = ((ListItem)(cbGiaoVien.SelectedItem)).value;
            giaovien_Data.thongtin_GiaoVien(dtgGiaoVien, magiaovien);
        }

        private void cbDeTai_SelectedIndexChanged(object sender, EventArgs e)
        {
            String madetai = ((ListItem)(cbDeTai.SelectedItem)).value;
            detai_Data.hienthi_DeTai(dtgDeTai, madetai);
        }

        private void txtMaNXDT_MouseClick(object sender, MouseEventArgs e)
        {
            txtMaNXDT.Text = "NXDT";
            txtDanhGia.Text = "";
            txtGhiChu.Text = "";
            txtSoTietChuan.Text = "";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
        }

        private void rdTenDeTai_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTDT.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenDeTai.Checked)
            {
                txtTGV.Visible = false;
                txtTDT.Visible = true;
                txtTDT.Text = "";
            }

        }

        private void rdTenGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTDT.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenDeTai.Checked)
            {
                txtTGV.Visible = false;
                txtTDT.Visible = true;
                txtTDT.Text = "";
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTenDeTai.Checked)
            {
                nxdetai_Data.timkiemtheo_TenDeTai(dtgNXDT, txtTDT.Text.ToString());
                txtTDT.Text = "";
            }
            else
            {
                nxdetai_Data.timkiemtheo_TenGV(dtgNXDT, txtTGV.Text.ToString());
                txtTGV.Text = "";
            }
        }

       

       
    }
}
